<template>
	<div class="app-container">
		<el-tabs v-model="queryParams.type" class="center-tabs" type="card" @tab-click="tabClick">
			<el-tab-pane v-for="dict in feedback_type" :label="dict.label" :name="parseInt(dict.value)">
			</el-tab-pane>
		</el-tabs>

		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
			<el-form-item label="姓名" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="电话" prop="phone">
				<el-input v-model="queryParams.phone" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="是否处理" prop="isHandle">
				<el-select v-model="queryParams.isHandle" placeholder="请选择" clearable>
					<el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"/>
				</el-select>
			</el-form-item>
			<el-form-item label="是否推荐" prop="isShow">
				<el-select v-model="queryParams.isShow" placeholder="请选择" clearable>
					<el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
				</el-select>
			</el-form-item>
			<el-form-item label="处理人" prop="handleUser">
				<el-input v-model="queryParams.handleUser" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

<!--		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['cm:cmFeedback:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['cm:cmFeedback:edit']">处理</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['cm:cmFeedback:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['cm:cmFeedback:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>-->

		<el-table v-loading="loading" :data="cmFeedbackList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center"/>
			<el-table-column label="类型" align="center" prop="type" v-if="columnVisible('type')">
				<template #default="scope">
					<dict-tag :options="feedback_type" :value="scope.row.type"/>
				</template>
			</el-table-column>
			<el-table-column label="用户id" align="center" prop="userId" v-if="columnVisible('userId')"/>
			<el-table-column label="姓名" align="center" prop="name" v-if="columnVisible('name')"/>
			<el-table-column label="电话" align="center" prop="phone" v-if="columnVisible('phone')"/>
			<el-table-column label="反馈内容" align="center" prop="content" v-if="columnVisible('content')"/>
			<el-table-column label="回复" align="center" prop="reply" v-if="columnVisible('reply')"/>
			<el-table-column label="是否处理" align="center" prop="isHandle" v-if="columnVisible('isHandle')">
				<template #default="scope">
					<dict-tag :options="sys_yes_no" :value="scope.row.isHandle"/>
				</template>
			</el-table-column>
			<el-table-column label="是否推荐" align="center" prop="isShow" v-if="columnVisible('isShow')">
				<template #default="scope">
					<el-switch v-model="scope.row.isShow" :active-value="1" :inactive-value="0" @change="handleIsShow(scope.row)"></el-switch>
				</template>
			</el-table-column>
			<el-table-column label="处理人" align="center" prop="handleUser" v-if="columnVisible('handleUser')"/>
			<el-table-column label="处理时间" align="center" prop="handleTime" v-if="columnVisible('handleTime')"/>
			<el-table-column label="归属部门" align="center" prop="deptId" v-if="columnVisible('deptId')">
				<template #default="scope">
					<span>{{ deptName(scope.row.deptId) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="备注" align="center" prop="remark" v-if="columnVisible('remark')"/>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
				<template #default="scope">
					<el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['cm:cmFeedback:query']" v-noPermi="['cm:cmFeedback:edit']">查看</el-button>
					<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['cm:cmFeedback:edit']">处理</el-button>
					<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['cm:cmFeedback:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList"/>

		<!-- 添加或处理反馈信息对话框 -->
		<el-dialog :title="title" v-model="open" width="1200" append-to-body>
			<el-form ref="cmFeedbackRef" :model="form" :rules="rules" label-width="80px" :disabled="!isInsert && !isEdit">
				<el-row>
					<el-form-item label="类型" prop="type">
						<el-select v-model="form.type" placeholder="请选择" >
							<el-option v-for="dict in feedback_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
						</el-select>
					</el-form-item>
					<el-col :span="8" v-if="isInsert">
						<el-form-item label="用户id" prop="userId">
							<el-input v-model="form.userId" placeholder="请输入" :disabled="!isInsert"/>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="姓名" prop="name">
							<el-input v-model="form.name" placeholder="请输入" :disabled="!isInsert"/>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="电话" prop="phone">
							<el-input v-model="form.phone" placeholder="请输入" :disabled="!isInsert"/>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="归属部门" prop="deptId">
							<el-tree-select v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择归属部门" check-strictly default-expand-all filterable :disabled="!checkPermi(['admin'])"/>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="反馈内容" prop="content">
							<el-input v-model="form.content" type="textarea" :rows="6" placeholder="请输入" :readonly="!isInsert"/>
<!--							<editor v-if="isInsert" v-model="form.content" :min-height="192" />-->
<!--							<div  v-else class="ql-editor" v-html="form.content" style="border: 1px solid #ddd; width: 100%"></div>-->
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="是否处理" prop="isHandle">
							<el-radio-group v-model="form.isHandle">
								<el-radio v-for="dict in sys_yes_no" :key="dict.value" :label="parseInt(dict.value)" :disabled="form.isHandleRaw > 0">{{ dict.label }}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="是否推荐" prop="isShow">
							<el-radio-group v-model="form.isShow" >
								<el-radio v-for="dict in sys_yes_no" :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="form.handleUser">
						<el-form-item label="处理人" prop="handleUser">
							<el-input v-model="form.handleUser" placeholder="请输入" disabled/>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="form.handleTime">
						<el-form-item label="处理时间" prop="handleTime">
							<el-date-picker clearable v-model="form.handleTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择" disabled/>
						</el-form-item>
					</el-col>

					<el-col :span="24">
						<el-form-item label="回复" prop="reply">
							<el-input v-model="form.reply" type="textarea" placeholder="请输入"/>
						</el-form-item>
					</el-col>
<!--					<el-col :span="24">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="form.remark" type="textarea" placeholder="请输入"/>
						</el-form-item>
					</el-col>-->
				</el-row>
			</el-form>
			<template #footer v-if="isEdit || isInsert">
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="CmFeedback">
import {listCmFeedback, getCmFeedback, delCmFeedback, addCmFeedback, updateCmFeedback} from "@/api/cm/cmFeedback";
import useDeptStore from "@/store/modules/dept.js";
import {checkPermi} from "@/utils/permission.js";
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import {changeJobStatus} from "@/api/tool/job.js";

const {proxy} = getCurrentInstance();
const { feedback_type, sys_yes_no } = proxy.useDict('feedback_type', 'sys_yes_no');

const deptStore = useDeptStore();

const cmFeedbackList = ref([]);
const isEdit = ref(false);
const isInsert = ref(false);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// 列显隐信息(able表示列是否可用，visible用于用户控制显隐)
const columns = ref([
	{label: `主键`, able: false, visible: false, key: 'id'},
	{label: `用户id`, able: false, visible: true, key: 'userId'},
	{label: `姓名`, able: true, visible: true, key: 'name'},
	{label: `电话`, able: true, visible: true, key: 'phone'},
	{label: `反馈内容`, able: false, visible: true, key: 'content'},
	{label: `回复`, able: true, visible: true, key: 'reply'},
	{label: `是否处理`, able: true, visible: true, key: 'isHandle'},
	{label: `是否推荐`, able: true, visible: true, key: 'isShow'},
	{label: `类型`, able: true, visible: true, key: 'type'},
	{label: `处理人`, able: true, visible: true, key: 'handleUser'},
	{label: `处理时间`, able: true, visible: true, key: 'handleTime'},
	{label: `部门id`, able: true, visible: true, key: 'deptId'},
	{label: `创建者`, able: false, visible: false, key: 'createBy'},
	{label: `创建时间`, able: false, visible: false, key: 'createTime'},
	{label: `更新者`, able: false, visible: false, key: 'updateBy'},
	{label: `更新时间`, able: false, visible: false, key: 'updateTime'},
	{label: `备注`, able: false, visible: true, key: 'remark'},
]);

const data = reactive({
	form: {},
	queryParams: {
		pageNum: 1,
		pageSize: 10,
		params: {},
		userId: null,
		name: null,
		phone: null,
		isHandle: null,
		isShow: null,
		type: 1,
		handleUser: null,
		deptId: null,
	},
	rules: {
		userId: [{required: true, message: "用户id不能为空", trigger: "blur"}],
		content: [{required: true, message: "反馈内容不能为空", trigger: "blur"}],
		name: [{required: true, message: "姓名不能为空", trigger: "blur"}],
		phone: [{required: true, message: "电话不能为空", trigger: "blur"}],
	}
});

const {queryParams, form, rules} = toRefs(data);

function tabClick(p, e) {
	queryParams.value.type = p.props.name
	queryParams.value.pageNum = 1
	getList();
}

/** 列显隐 */
function columnVisible(key) {
	return proxy.checkColumn(columns.value, key);
}

/** 查询反馈信息列表 */
function getList() {
	loading.value = true;
	listCmFeedback(queryParams.value).then(response => {
		cmFeedbackList.value = response.rows;
		total.value = response.total;
		loading.value = false;
	});
}

// 取消按钮
function cancel() {
	open.value = false;
	reset();
}

// 表单重置
function reset() {
	form.value = {
		id: null,
		userId: null,
		name: null,
		phone: null,
		content: null,
		reply: null,
		isHandle: 0,
		isShow: 0,
		type: queryParams.value.type,
		handleUser: null,
		handleTime: null,
		deptId: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
		remark: null
	};
	proxy.resetForm("cmFeedbackRef");
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}

/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm("queryRef");
	handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map(item => item.id);
	single.value = selection.length != 1;
	multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = "添加反馈信息";
	isInsert.value = true;
	isEdit.value = false;
}

/** 查看按钮操作 */
function handleView(row) {
	isEdit.value = false;
	title.value = "查看反馈信息";
	getInfo(row);
}

/** 处理按钮操作 */
function handleUpdate(row) {
	isEdit.value = true;
	title.value = "处理反馈信息";
	getInfo(row);
}

function getInfo(row) {
	reset();
	const _id = row.id || ids.value
	isInsert.value = false;
	proxy.modal.loading("加载中");
	getCmFeedback(_id).then(response => {
		form.value = response.data;
		form.value.isHandleRaw = response.data.isHandle;
		open.value = true;
		proxy.modal.closeLoading();
	}).catch(err => {
		Promise.reject(err);
		proxy.modal.closeLoading();
	});
}

/** 提交按钮 */
const cmFeedbackRef = ref(null)

function submitForm() {
	cmFeedbackRef.value.validate(valid => {
		if (valid) {
			if (form.value.id != null) {
				proxy.modal.loading("提交中");
				updateCmFeedback(form.value).then(response => {
					proxy.modal.msgSuccess("处理成功");
					open.value = false;
					getList();
					proxy.modal.closeLoading();
				}).catch(err => {
					proxy.modal.closeLoading();
					Promise.reject(err);
				});
			} else {
				proxy.modal.loading("提交中");
				addCmFeedback(form.value).then(response => {
					proxy.modal.msgSuccess("新增成功");
					open.value = false;
					getList();
					proxy.modal.closeLoading();
				}).catch(err => {
					proxy.modal.closeLoading();
					Promise.reject(err);
				});
			}
		}
	});
}

function handleIsShow(row) {
	let text = row.isShow ? "推荐" : "不推荐";
	proxy.modal.confirm('确认要"' + text + '反馈到小程序吗?').then(function () {
		return updateCmFeedback({id:row.id,isShow:row.isShow});
	}).then(() => {
		proxy.modal.msgSuccess(text + "成功");
	}).catch(function () {
		row.status = row.status ? 0 : 1;
	});
}

/** 删除按钮操作 */
function handleDelete(row) {
	const _ids = row.id || ids.value;
	proxy.modal.confirm('是否确认删除反馈信息编号为"' + _ids + '"的数据项？').then(function () {
		proxy.modal.loading("删除中");
		return delCmFeedback(_ids);
	}).then(() => {
		getList();
		proxy.modal.msgSuccess("删除成功");
		proxy.modal.closeLoading();
	}).catch(() => {
		proxy.modal.closeLoading();
	});
}

/** 导出按钮操作 */
function handleExport() {
	proxy.download.post('cm/cmFeedback/export', {
		...queryParams.value
	}, `cmFeedback_${new Date().getTime()}.xlsx`)
}

/** 查询部门下拉树结构 */
const deptOptions = ref(undefined);
watchEffect(() => {
	deptOptions.value = deptStore.deptTree;
});


getList();
</script>
